package hotel.switchs.oversea.biz.controller;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.StringUtil;
import hotel.switchs.oversea.api.entity.SwitchsDelayAgainDealLog;
import hotel.switchs.oversea.api.entity.order.SwitchOrderListNotify;
import hotel.switchs.oversea.biz.mapper.SwitchOrderInfoMapper;
import hotel.switchs.oversea.biz.mapper.SwitchOrderListNotifyMapper;
import hotel.switchs.oversea.biz.mapper.SwitchsDelayAgainDealLogMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/switchsDelayAgainDealLog")
@Api(value = "switchsDelayAgainDealLogController",tags = {"延迟单每一分钟处理日志"})
public class switchsDelayAgainDealLogController {

    @Autowired
    SwitchsDelayAgainDealLogMapper switchsDelayAgainLogDealMapper;

    @Autowired
    SwitchOrderListNotifyMapper switchOrderListNotifyMapper;

    @Autowired
    SwitchOrderInfoMapper switchOrderInfoMapper;

    @PostMapping("/saveByMd5key")
    @ApiOperation(value = "保存日志根据md5key接口",notes="必须经过auth授权")
    public Result<Object> saveByMd5key(@RequestBody Map<String,String> map){
        String md5Key = map.get("md5key");
        String json = map.get("remark");

        Example example = new Example(SwitchOrderListNotify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("md5Key",md5Key);
        SwitchOrderListNotify switchOrderListNotify = switchOrderListNotifyMapper.selectOneByExample(example);
        if(ObjectUtils.isEmpty(switchOrderListNotify)){
            return new Result<>(md5Key+",未找到notify数据",CommonConstants.ERROR);
        }
        //1：先根据订单号，获取订单详细信息，并做订单详情数据新增；为了获取到对应酒店id 和房型id
        String orderId = switchOrderListNotify.getOrderId();//订单id

        SwitchsDelayAgainDealLog switchsDelayAgainDealLog = new SwitchsDelayAgainDealLog();
        switchsDelayAgainDealLog.setId(StringUtil.getUUID());
        switchsDelayAgainDealLog.setSwOrderId(orderId);
        switchsDelayAgainDealLog.setCreateTime(new Date());
        switchsDelayAgainDealLog.setRemark(json);
        switchsDelayAgainLogDealMapper.insert(switchsDelayAgainDealLog);
        return new Result();
    }
}
